﻿using System.Windows;
using System.Windows.Controls;

namespace CountJobTags {
    /// <summary>
    /// Interaction logic for DataBoundSmiley.xaml
    /// </summary>
    public partial class DataBoundSmiley : UserControl {
        private static string NO_SMILE = "M 5,12.5 A 10,0 0 0 0 7.5, 12.5 Z";
        public DataBoundSmiley() {
            InitializeComponent();
        }

        public double SmilePct {
            get { return (double)GetValue(SmilePctProperty); }
            set { SetValue(SmilePctProperty, value); }
        }

        public static readonly DependencyProperty SmilePctProperty =
            DependencyProperty.Register("SmilePct", typeof(double), typeof(DataBoundSmiley), new PropertyMetadata(0.0, OnSmileyPctChanged));

        public static void OnSmileyPctChanged(object sender, DependencyPropertyChangedEventArgs e) {
            DataBoundSmiley theInstance = (DataBoundSmiley)sender;
            double theVal = (double)e.NewValue;
            //protect against bad data:
            if (theVal < 0)
                theVal = 0.0;
            else if (theVal > 1.0)
                theVal = 1.0;

            double xInit = 5 - (5.0 * theVal);
            double yInit = 12.5 - (2.5 * theVal);
            double pctHappy = (25 * theVal);
            double xTerm = 7.5 + (5.0 * theVal);
            double yTerm = 12.5 - (2.5 * theVal);
            //bigSmile  = "M 0,10   A 10,25  0 0 0 12.5,10   Z";
            //noSmile   = "M 5,12.5 A 10,0   0 0 0 7.5, 12.5 Z";
            //Path geometry starts with M x,y, where x,y is the start coordinates
            //Next we have an Arc segment
            //ArcSegment has form A rx,ry d f1 f2 x,y F
            //where A      → Start Arc
            //      rx, ry → x and y radius for the ellipse defining your arc segment
            //      d      → degrees to rotate
            //      f1     → flag/IsLargArc
            //      f2     → flag/Clockwise
            //      x,y    → End coordinates of the segment
            //      Z      → Optional, close figure
            theInstance.GeometryString = string.Format("M {0}, {1} A 10,{2} 0 0 0 {3},{4} Z",
                                                       xInit, yInit, pctHappy, xTerm, yTerm);
        }

        public string GeometryString {
            get { return (string)GetValue(GeometryStringProperty); }
            set { SetValue(GeometryStringProperty, value); }
        }

        public static readonly DependencyProperty GeometryStringProperty =
            DependencyProperty.Register("GeometryString", typeof(string), typeof(DataBoundSmiley), new PropertyMetadata(NO_SMILE));
    }
}
